<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/25
 * Time: 21:40
 */

namespace My;


class Redis
{
    private static $handler;

    private static function handler(){
        if(!self::$handler){
            self::$handler = new \Redis();
            self::$handler -> connect('127.0.0.1','6379');
        }
        return self::$handler;
    }

    public static function get($key){
        $value = self::handler() -> get($key);
        $value_serl = @unserialize($value);
        if(is_object($value_serl)||is_array($value_serl)){
            return $value_serl;
        }
        return $value;
    }

    public static function set($key,$value,$time='nu'){

        if(is_object($value)||is_array($value)){
            $value = serialize($value);
        }

        if($time == 'nu'){
        	return self::handler() -> set($key,$value);
        }else{
        	return self::handler() -> setex($key,$time,$value);
        }
    }

    // $keys 可以为单个key或key数组，返回删除个数
    public static function del($keys){
        return self::handler() -> delete($keys);
    }


}